<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ZhangYi
 * Date: 11-9-2
 * Time: 上午8:17
 * To change this template use File | Settings | File Templates.
 */

class celebrityVoteComponents extends sfComponents
{

    public function executeEntry(sfWebRequest $request)
    {
        $this->top3males = Doctrine_Core::getTable('celebrityvote')
                ->createQuery('c')
                ->addWhere('c.theme=?', Constants::$VOTE_THEME_MALE)->orderBy('(c.vote_i + c.vote_mask_i) desc')->limit(3)
                ->execute();

        $this->top3females = Doctrine_Core::getTable('celebrityvote')
                ->createQuery('c')
                ->addWhere('c.theme=?', Constants::$VOTE_THEME_FEMALE)->orderBy('(c.vote_i + c.vote_mask_i) desc')->limit(3)
                ->execute();

        $this->top3bands = Doctrine_Core::getTable('celebrityvote')
                ->createQuery('c')
                ->addWhere('c.theme=?', Constants::$VOTE_THEME_BAND)->orderBy('(c.vote_i + c.vote_mask_i) desc')->limit(3)
                ->execute();
    }

    public function executeCandidateList()
    {
        //        $theme = $request->getParameter("theme");
        $theme = $this->theme;
        switch ($theme)
        {
            case "male":
                $theme = "男偶像投票";
                break;
            case "female":
                $theme = "女偶像投票";
                break;
            case "band":
                $theme = "组合投票";
                break;
        }
        $this->voteCandidates = @Doctrine_Core::getTable("celebrityvote")->createQuery("v")
                ->addWhere('v.theme=?', $theme)->addOrderBy('order_i')->limit(Constants::$CELEBRITY_VOTE_NUM)->execute();
    }

    public function executeVoteResult(sfWebRequest $request)
    {
        $theme = $request->getParameter("theme");
        switch ($theme)
        {
            case "male":
                $theme = "男偶像投票";
                break;
            case "female":
                $theme = "女偶像投票";
                break;
            case "band":
                $theme = "组合投票";
                break;
        }
        $voteCandidates = @Doctrine_Core::getTable("celebrityvote")->createQuery("v")
                ->addWhere('v.theme=?', $theme)
                ->addOrderBy('v.vote_i+v.vote_mask_i desc')
                ->limit(Constants::$CELEBRITY_VOTE_NUM)->execute();


        $totalVote = 0;
        foreach ($voteCandidates as $voteCandidate) {
            $totalVote += $voteCandidate->getVoteI();
            $totalVote += $voteCandidate->getVoteMaskI();
        }

        $votePercentage = array();
        foreach ($voteCandidates as $voteCandidate) {
            $votePercentage[$voteCandidate->getId()] = (float)($voteCandidate->getVoteI() + $voteCandidate->getVoteMaskI()) / (float)$totalVote;
        }

        $this->votePercentage = $votePercentage;
        $this->voteCandidates = $voteCandidates;
    }
}
